package Mission

import (
	codecs "sanguo/codec/cs"
	"sanguo/node/common/com_type"
	"sanguo/node/common/transaction"
	"sanguo/node/node_game"
	"sanguo/node/node_game/module/role"
	"sanguo/node/node_game/user"
	cs_message "sanguo/protocol/cs/message"
	"time"

	"github.com/golang/protobuf/proto"
)

type transactionTalkToNpc struct {
	transaction.TransactionBase
	user    *user.User
	req     *codecs.Message
	resp    *cs_message.TalkToNpcToC
	errcode cs_message.EnumType
}

func (this *transactionTalkToNpc) GetModuleName() string {
	return "Mission"
}

func (this *transactionTalkToNpc) GetReplyMsg() proto.Message {
	return &cs_message.TalkToNpcToC{}
}

func (this *transactionTalkToNpc) SetExpiredTime(expired time.Time) {
	this.Expired = expired
}

func (this *transactionTalkToNpc) GetExpiredTime() time.Time {
	return this.Expired
}

func (this *transactionTalkToNpc) Begin() {

	this.resp = &cs_message.TalkToNpcToC{}
	msg := this.req.GetData().(*cs_message.TalkToNpcToS)
	roleModule := this.user.GetModuleByName("role").(*role.RoleModule)
	role := roleModule.CurrentRole()
	if role == nil || !role.IsRoleModleData() {
		node_game.Infoln(role.GetUserID(), "TalkToNpcToC fail : role is nil")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}
	node_game.Infof("%s %d Call TalkToNpc id:%d condid:%d", role.GetUserID(), role.GetID(), msg.GetMissonId(), msg.GetCondID())

	mission := role.GetRoleMission()
	if mission == nil {
		node_game.Debugln(role.GetUserID(), role.GetID(), "TalkToNpcToC fail :no mission")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}

	if mission.GetMission(msg.GetMissonId()).GetMissionCond(msg.GetCondID()) == nil {
		node_game.Debugln(role.GetUserID(), role.GetID(), "TalkToNpcToC fail :mission cond is fail")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}

	//int32(3) mission_running
	if mission.GetMission(msg.GetMissonId()).GetMissionState() != int32(3) {
		node_game.Debugln(role.GetUserID(), role.GetID(), "TalkToNpcToC fail :mission do not accept")
		this.errcode = cs_message.EnumType_ERROR
		this.user.EndTrans(this)
		return
	}

	mission.AddMissionCond(msg.GetMissonId(), com_type.Mission_TalkToNpc, msg.GetCondID(), int32(1))
	//	if !mission.AcceptMission(msg.GetMissionID()) {
	//		node_game.Infoln(roleModule.CurrentRole().GetID(), "AcceptMission fail")
	//		this.errcode = cs_message.EnumType_ERROR
	//		this.user.EndTrans(this)
	//		return
	//	}

	node_game.Infoln(role.GetUserID(), role.GetID(), "TalkToNpc OK")
	this.errcode = cs_message.EnumType_OK
	this.user.EndTrans(this)
}

func (this *transactionTalkToNpc) End() {
	this.resp.Errcode = cs_message.EnumType(this.errcode).Enum()
	this.user.Reply(this.req.GetSeriNo(), this.resp)
}

func (this *transactionTalkToNpc) Timeout() {
	this.resp.Errcode = cs_message.EnumType(cs_message.EnumType_RETRY).Enum()
	this.user.EndTrans(this)
}

func init() {
	user.RegisterTransFunc(&cs_message.TalkToNpcToS{}, func(user *user.User, msg *codecs.Message) transaction.Transaction {
		return &transactionTalkToNpc{
			user: user,
			req:  msg,
		}
	})
}
